package com.dm0858.bianmin.ui.activity;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.dm0858.bianmin.R;
import com.lidroid.xutils.BitmapUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class GridAdapter extends ArrayAdapter {

        private final int resourceId;
        private Context contexts;
        private int viewWidth;
        private int viewHeight;
        private Activity activity;
        public List<String> upPathList = new ArrayList<String>();

    public GridAdapter(Context context, int textViewResourceId, List<String> objects,int width,int height,Activity activity) {
            super(context, textViewResourceId, objects);
            contexts = context;
            resourceId = textViewResourceId;
            viewWidth = width;
            viewHeight = height;
            this.activity = activity;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyViewHolder holder = null;


//L.e("screenWidth:" + screenWidth);
            if(convertView == null){
                convertView = LayoutInflater.from(getContext()).inflate(resourceId, null);//实例化一个对象
                AbsListView.LayoutParams params = new AbsListView.LayoutParams(viewWidth,viewHeight);
                holder = new MyViewHolder();
                holder.photo = convertView.findViewById(R.id.photoImageView);
                convertView.setLayoutParams(params);

                convertView.setTag(holder);

            }else {
                holder = (MyViewHolder) convertView.getTag();
            }




            String path = (String) getItem(position); // 获取当前项的popupBean实例
            if (path.equals("-1")) {
                Resources r = contexts.getResources();

                Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.paizhao);
                holder.photo.setImageBitmap(bmp);
            }else{

                // 读取uri所在的图片
                BitmapUtils bitmapUtils = new BitmapUtils(contexts);
                bitmapUtils.configDefaultShowOriginal(false);
                bitmapUtils.configDefaultAutoRotation(true);
                bitmapUtils.configDefaultBitmapMaxSize(holder.photo.getLayoutParams().width, holder.photo.getLayoutParams().height);

                bitmapUtils.display(holder.photo, path);


            }






            return convertView;
        }







    public static class MyViewHolder {
        ImageView photo;
    }

}


